package com.hnucm.c201901020241.my;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hnucm.c201901020241.LoginActivity;
import com.hnucm.c201901020241.R;
import com.hnucm.c201901020241.discuss.Discuss_post_information;
import com.hnucm.c201901020241.pojo.User;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class Update_name extends AppCompatActivity {
    String name,ac,psw,base64,signature,sex,birthday,grade,college;
    TextView numChage;
    EditText editText;
//    广播的标识
    public static final String action = "update_name";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_name);
        editText=findViewById(R.id.textView61);
        numChage=findViewById(R.id.textView62);
        SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_MULTI_PROCESS);

        ac= sharedPreferences.getString("account","");
        psw= sharedPreferences.getString("password","");
        name= sharedPreferences.getString("name", "null");
        base64= sharedPreferences.getString("base64","");
        signature= sharedPreferences.getString("signature","");
        sex= sharedPreferences.getString("sex","");
        birthday= sharedPreferences.getString("birthday","");
        grade= sharedPreferences.getString("grade","");
        college= sharedPreferences.getString("college","");
//        editText.setText(name);
        findViewById(R.id.imageView19).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.textView64).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name_new=editText.getText().toString();
                if(name_new==null){
                    Toast.makeText(Update_name.this,"昵称不能为空",Toast.LENGTH_SHORT).show();
                }
                else{
//                    User user=new User(ac,psw,name_new,signature,sex,birthday,grade,college,base64);
                    SharedPreferences.Editor editor=sharedPreferences.edit();
                    editor.putString("name",name_new);
                    editor.commit();
                    x.Ext.init(getApplication());
                    RequestParams requestParams = new RequestParams("http://121.4.27.254:8090/updateUser?");
                    requestParams.addQueryStringParameter("account",ac);
                    requestParams.addQueryStringParameter("password",psw);
                    requestParams.addQueryStringParameter("name",name_new);
//                    requestParams.addQueryStringParameter("base64",base64);
                    requestParams.addQueryStringParameter("signature",signature);
                    requestParams.addQueryStringParameter("sex",sex);
                    requestParams.addQueryStringParameter("birthday",birthday);
                    requestParams.addQueryStringParameter("grade",grade);
                    requestParams.addQueryStringParameter("college",college);
                    x.http().post(requestParams, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {
                            if (result.equals("1")) {
                                Toast.makeText(Update_name.this, "修改成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(action);
                                intent.putExtra("name", name_new);
                                sendBroadcast(intent);
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            Toast.makeText(Update_name.this, "网络出现异常错误，请稍后", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onCancelled(CancelledException cex) {
                        }

                        @Override
                        public void onFinished() {

                        }
                    });
                }
            }
        });
//        统计字数
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                numChage.setText(editText.getText().length()+"/10");
            }

            @Override
            public void afterTextChanged(Editable s) {
                numChage.setText(editText.getText().length()+"/10");
            }
        });
    }
}